package code.android.remember;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ViewFlipper;

public class MainActivity extends ListActivity
{
	public static final String DEBUG_TAG = "MainActivity";
	private final int FLIP_VELOCITY = 700;
	
	ViewFlipper mViewFlipper = null;
	ArrayAdapter<String> mArrayAdapter = null;
	ArrayList<String> mWordList = new ArrayList<String>();
	EditText mEditText = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, mWordList);
        
        setListAdapter(mArrayAdapter);
        
        mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        Button button1  = (Button) findViewById(R.id.Button01);
        Button button2 = (Button) findViewById(R.id.Button02);
        mEditText = (EditText) findViewById(R.id.EditText01);
        
        button1.setOnClickListener( new View.OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				mViewFlipper.setInAnimation(inFromRightAnimation());
				mViewFlipper.setOutAnimation(outToLeftAnimation());
				mViewFlipper.showNext();
				
			}
		});
        
        button2.setOnClickListener(new View.OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				mViewFlipper.setInAnimation(inFromLeftAnimation());
				mViewFlipper.setOutAnimation(outToRightAnimation());
				mViewFlipper.showNext();
			}
		});
        
        mEditText.setOnClickListener( new View.OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				sendText();
			}
		});
        
        mEditText.setOnKeyListener( new View.OnKeyListener() 
        {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) 
			{
				if(event.getAction() == KeyEvent.ACTION_DOWN)
				{
					switch (keyCode) 
					{
						case KeyEvent.KEYCODE_ENTER:
						{
							sendText();
							return true;
						}
					}
				}
				return false;
			}
		});
        
        Log.i(DEBUG_TAG, "OnCreate()");
    }
    
    private void sendText()
    {
		String text = mEditText.getText().toString();
		
		if(text.length() == 0)
		{
			return;
		}
		mArrayAdapter.add(text);
		mEditText.setText(null);
    }
    
    @Override
    protected void onPause() 
    {
    	Log.i(DEBUG_TAG, "OnPause()");
    	super.onPause();
    }
    
    @Override
    protected void onStart() 
    {
    	Log.i(DEBUG_TAG, "OnStart()");
    	super.onStart();
    }
    
    @Override
    protected void onResume() 
    {
    	Log.i(DEBUG_TAG, "OnResume()");
    	super.onResume();
    }
    
    @Override
    protected void onStop() 
    {
    	Log.i(DEBUG_TAG, "OnStop()");
    	super.onStop();
    }
    
    @Override
    protected void onRestart() 
    {
    	Log.i(DEBUG_TAG, "OnRestart()");
    	super.onRestart();
    }
    
    @Override
    protected void onDestroy() 
    {
    	Log.i(DEBUG_TAG, "OnDestroy()");
    	super.onDestroy();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
    	Log.i(DEBUG_TAG, "OnConfigurationChanged()");
    	super.onConfigurationChanged(newConfig);
    }
    
    
    private Animation inFromRightAnimation()
    {
		Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, 
					Animation.RELATIVE_TO_PARENT, 0.0f, 
					Animation.RELATIVE_TO_PARENT, 0.0f, 
					Animation.RELATIVE_TO_PARENT, 0.0f
					);
		
		inFromRight.setDuration(FLIP_VELOCITY);
		
		inFromRight.setInterpolator(new AnticipateInterpolator());
		
		return inFromRight;

    }
    
    private Animation outToLeftAnimation()
    {
		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f
				);
		
		outtoLeft.setDuration(FLIP_VELOCITY);
		
		outtoLeft.setInterpolator(new AnticipateInterpolator());
		
		return outtoLeft;
    }
    
    private Animation inFromLeftAnimation()
    {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f
				);
		inFromLeft.setDuration(FLIP_VELOCITY);
		
		inFromLeft.setInterpolator(new AnticipateInterpolator());
		
		return inFromLeft;
    }
    
    private Animation outToRightAnimation() 
    {
		Animation outtoRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f
				);
		
		outtoRight.setDuration(FLIP_VELOCITY);
		
		outtoRight.setInterpolator(new AnticipateInterpolator());
		
		return outtoRight;
	}
    
   
    
}